package com.xiaochong.tir.apiservice.scheduled;

import java.io.IOException;
import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.xc.logclient.utils.LogTrace;
import com.xiaochong.tir.apiservice.component.HbaseBaseQueryComponent;
import com.xiaochong.tir.common.data.hbasepo.WeightIndex;
import com.xiaochong.tir.common.data.mapper.KLineMapper;
import com.xiaochong.tir.common.data.po.KLine;
import com.xiaochong.tir.common.exception.hbase.HbaseCloseExcetion;
import com.xiaochong.tir.common.exception.hbase.HbaseConnectionExcetion;
import com.xiaochong.tir.common.exception.hbase.HbaseGetTableExcetion;
import com.xiaochong.tir.common.util.DateUtils;
import com.xiaochong.tir.common.util.HbaseTableUtils;
import com.xiaochong.tir.common.util.KLineTypeEnum;

/**
 * 计算昨日权重指数日K线定时任务
 * @author qzw
 * 2018年6月26日 下午2:29:55
 * @description 
 **/
@Component
public class WeightIndexKlineIndex {
	
	@Autowired
    private HbaseBaseQueryComponent hbaseBaseQueryComponent;
	
	@Autowired
	private KLineMapper kLineDao;
	
	@Scheduled(fixedRate = 1000 * 60 * 24 * 365)
	public void getMonKline(){
		LogTrace.beginTrace();
		for(int i = 1; i < 90; i++){
			try {
				String yesterday = DateUtils.getNextDayDateYMD(-i);
				String data = hbaseBaseQueryComponent.selectByQualifierData(HbaseTableUtils.HOME_INDEX_EVERY_DAY_RECORDING_TABLE_NAME
				        , HbaseTableUtils.HOME_INDEX_EVERY_DAY_RECORDING_TABLE_FAMILY
				        , HbaseTableUtils.CALCULATE_TICKER_QUALIFIER
				        , HbaseTableUtils.HOME_INDEX_EVERY_DAY_WEIGHTINDEX_ROWKEY+yesterday);
				if(data != null && !"".equals(data.trim())){
					preKline(-i, data);
				}
			} catch (HbaseConnectionExcetion | HbaseCloseExcetion | IOException | HbaseGetTableExcetion e) {
				e.printStackTrace();
			} 
		}
	}

	/**
	 * 每天凌晨00:15计算昨天的权重指数日K线
	 */
	@Scheduled(cron="0 15 0 * * ?")
	public void wiKline(){
		LogTrace.info("权重指数K线", "计算昨日的K线数据", "昨日K线数据计算开始");
		try {
			String yesterday = DateUtils.getNextDayDateYMD(-1);
			String data = hbaseBaseQueryComponent.selectByQualifierData(HbaseTableUtils.HOME_INDEX_EVERY_DAY_RECORDING_TABLE_NAME
			        , HbaseTableUtils.HOME_INDEX_EVERY_DAY_RECORDING_TABLE_FAMILY
			        , HbaseTableUtils.CALCULATE_TICKER_QUALIFIER
			        , HbaseTableUtils.HOME_INDEX_EVERY_DAY_WEIGHTINDEX_ROWKEY+yesterday);
			if(data != null && !"".equals(data.trim())){
				preKline(-1, data);
			}
		} catch (HbaseConnectionExcetion | HbaseCloseExcetion | IOException | HbaseGetTableExcetion e) {
			e.printStackTrace();
		} 
		LogTrace.setPrintLocalLog(true);
		LogTrace.setPushLog(false);
	}
	
	/**
	 * 计算K线并入库
	 * @param data
	 */
	public void preKline(int day, String data){
		JSONArray ja = JSONArray.parseArray(data);
		if(ja.size() > 0){
			//获取开盘权重指数
			JSONObject objws = (JSONObject)ja.get(0);
			WeightIndex ws = objws.toJavaObject(WeightIndex.class);
			BigDecimal openindex = ws.getWeightIndex();
			//获取收盘权重指数
			JSONObject objwe = (JSONObject)ja.get(ja.size() - 1);
			WeightIndex we = objwe.toJavaObject(WeightIndex.class);
			BigDecimal closeindex = we.getWeightIndex();
			//初始化高开权重指数和低收权重指数
			BigDecimal highopen = ws.getWeightIndex();
			BigDecimal lowclose = ws.getWeightIndex();
			for(int i = 0; i < ja.size(); i++){
				JSONObject obj = (JSONObject)ja.get(i);
				WeightIndex w = obj.toJavaObject(WeightIndex.class);
				BigDecimal big = w.getWeightIndex();
				if(big.compareTo(lowclose) == -1){
					//获取最小权重指数
					lowclose = big;
				}else if(big.compareTo(highopen) == 1){
					//获取最大权重指数
					highopen = big;
				}
			}
			String yes = LocalDateTime.now().plusDays(day).format(DateUtils.format_ymd);
			KLine sk = new KLine();
			sk.setKType(KLineTypeEnum.DAY_WEIGHT_INDEX.getIndex() + "");
			sk.setKDate(yes);
			List<KLine> l = kLineDao.selectListByNameAndDate(sk);
			if(l.size() == 0){
				LocalDateTime nowDate = LocalDateTime.now();
				Date now = DateUtils.localDateTimeToDate(nowDate);
				KLine k = new KLine();
				k.setCreateTime(now);
				k.setUpdateTime(now);
				k.setClosePrice(closeindex + "");
				k.setOpenPrice(openindex + "");
				k.setHighOpen(highopen + "");
				k.setLowClose(lowclose + "");
				k.setKDate(yes);
				k.setKName(KLineTypeEnum.DAY_WEIGHT_INDEX.getName());
				k.setKType(KLineTypeEnum.DAY_WEIGHT_INDEX.getIndex() + "");
				k.setVolume("0");
				kLineDao.insert(k);
			}
		}else{
			LogTrace.info("权重指数K线", "权重指数数据", "day:" + day + ",data" + data + ",今日日期：" + DateUtils.getCurrentDateString());
		}
	}
}
